package org.leumasjaffe.initiativeTracker.entity.trap;

import java.util.ArrayList;

import mathematics.probability.AbstractRV;

import org.eclipse.jdt.annotation.NonNull;

/**
 * An abstract of a trap that uses a RandomVariable for some part of its functionality
 * @author Sam Jaffe
 *
 */
public abstract class RandomTrap extends Trap{
	protected final AbstractRV[] r;
	
	RandomTrap (@NonNull AbstractRV... r) {
		this.r = r;
	}
	
	/**
	 * Decodes a trap from a string describing it
	 * @param s The String holding the data on the trap
	 * @return A constructed trap
	 */
	public static final RandomTrap decodeTrap(@NonNull String s) {
		RandomTrap temp = null;
		if (s.startsWith("SwingingBladeTrap"))
			temp = new SwingingBladeTrap(Integer.decode(s.substring(17)));
		if (s.startsWith("SpellBurstTrap"))
			if (s.length() > 14)
				temp = new SpellBurstTrap(Integer.decode(s.substring(14)));
			else
				temp = new SpellBurstTrap();
		return temp;
	}
	
	/**
	 * Shuffles an array based on a random variable
	 * @param <T> The object type that is being shuffled
	 * @param order A list of objects to shuffle
	 * @param random A random variable generator
	 * @return The shuffled array
	 */
	protected <T> ArrayList<T> shuffle(@NonNull ArrayList<T> order, @NonNull AbstractRV random) {	
		ArrayList<T> temp = new ArrayList<T>();
		while (order.size() > 0) {
			temp.add(order.remove((int) random.getX()));
		}
		return temp;
	}

}
